<template>
  <div class="member-exclusive-content">
    <!-- 会员可见内容 -->
    <div v-if="isMember" class="exclusive-content">
      <div class="content-header">
        <h3>🎯 会员专属内容</h3>
        <div class="membership-badge" :style="{ backgroundColor: membershipInfo?.color || '#cd7f32' }">
          {{ membershipInfo?.name || '会员' }}
        </div>
      </div>
      
      <div class="exclusive-body">
        <!-- 会员专享高清图片 -->
        <div class="exclusive-section">
          <h4>🖼️ 高清图片集</h4>
          <div class="image-gallery">
            <img v-for="(img, index) in additionalImages" :key="index" :src="img" alt="高清图片" class="gallery-image">
          </div>
        </div>
        
        <!-- 会员专享详细信息 -->
        <div class="exclusive-section">
          <h4>📚 详细百科</h4>
          <div class="detailed-info">
            <p v-for="(fact, index) in detailedFacts" :key="index" class="fact-item">
              <span class="fact-icon">🔍</span>
              {{ fact }}
            </p>
          </div>
        </div>
        
        <!-- 会员专享互动功能 -->
        <div class="exclusive-section">
          <h4>🎮 互动体验</h4>
          <div class="interactive-features">
            <button class="interactive-button" @click="saveToFavorites">
              ❤️ 添加到收藏
            </button>
            <button class="interactive-button" @click="downloadWallpaper">
              🖼️ 下载壁纸
            </button>
            <button class="interactive-button" @click="shareAnimal">
              📤 分享给好友
            </button>
          </div>
        </div>
        
        <!-- 会员专享评论区 -->
        <div class="exclusive-section">
          <h4>💬 会员讨论区</h4>
          <div class="comments-section">
            <div v-if="comments.length === 0" class="no-comments">
              暂无评论，来发表第一条评论吧！
            </div>
            <div v-else class="comments-list">
              <div v-for="comment in comments" :key="comment.id" class="comment-item">
                <div class="comment-avatar">{{ comment.user.charAt(0).toUpperCase() }}</div>
                <div class="comment-content">
                  <div class="comment-header">
                    <span class="comment-user">{{ comment.user }}</span>
                    <span class="comment-time">{{ comment.time }}</span>
                  </div>
                  <p class="comment-text">{{ comment.text }}</p>
                </div>
              </div>
            </div>
            
            <div class="comment-form">
              <textarea v-model="newComment" placeholder="分享您的想法..." class="comment-input"></textarea>
              <button @click="addComment" class="add-comment-button">发布评论</button>
            </div>
          </div>
        </div>
      </div>
    </div>
    
    <!-- 非会员提示 -->
    <div v-else class="non-member-content">
      <div class="lock-icon">🔒</div>
      <h3>会员专享内容</h3>
      <p class="locked-message">
        此内容仅对会员开放，升级会员即可解锁更多精彩内容！
      </p>
      <div class="membership-preview">
        <p class="preview-text">会员可以享受：</p>
        <ul class="privileges-preview">
          <li>✨ 高清图片集</li>
          <li>✨ 详细动物百科</li>
          <li>✨ 专属互动功能</li>
          <li>✨ 会员讨论区</li>
        </ul>
      </div>
      <router-link to="/membership-center" class="upgrade-button">
        立即升级会员
      </router-link>
    </div>
  </div>
</template>

<script setup>
import { ref, computed, onMounted } from 'vue'
import { useRouter } from 'vue-router'
import {
  isMember as checkIsMember,
  getMembershipInfo,
  getCurrentUser
} from '../services/membershipService.js'

// Props
const props = defineProps({
  animalId: {
    type: Number,
    required: true
  },
  additionalImages: {
    type: Array,
    default: () => []
  },
  detailedFacts: {
    type: Array,
    default: () => []
  }
})

const router = useRouter()
const isMemberUser = ref(false)
const membershipInfo = ref(null)
const comments = ref([])
const newComment = ref('')
const saveSuccess = ref(false)

// 计算属性
const isMember = computed(() => isMemberUser.value)

// 初始化组件
const initializeComponent = () => {
  isMemberUser.value = checkIsMember()
  membershipInfo.value = getMembershipInfo()
  
  // 加载评论（模拟数据）
  loadComments()
}

// 加载评论（模拟）
const loadComments = () => {
  // 模拟评论数据
  comments.value = [
    {
      id: 1,
      user: '动物爱好者',
      time: '2小时前',
      text: '这只动物真的太可爱了，我很喜欢它的毛色！'
    },
    {
      id: 2,
      user: '自然探索者',
      time: '昨天',
      text: '感谢分享这些详细信息，让我对这种动物有了更深的了解。'
    },
    {
      id: 3,
      user: '宠物主人',
      time: '3天前',
      text: '我也养了一只类似的宠物，真的很聪明很通人性！'
    }
  ]
}

// 添加评论
const addComment = () => {
  if (newComment.value.trim() === '') {
    return
  }
  
  const currentUser = getCurrentUser()
  if (!currentUser) {
    router.push('/login')
    return
  }
  
  const newCommentObj = {
    id: Date.now(),
    user: currentUser.username,
    time: '刚刚',
    text: newComment.value.trim()
  }
  
  comments.value.unshift(newCommentObj)
  newComment.value = ''
}

// 保存到收藏
const saveToFavorites = () => {
  saveSuccess.value = true
  setTimeout(() => {
    saveSuccess.value = false
  }, 2000)
}

// 下载壁纸
const downloadWallpaper = () => {
  if (props.additionalImages && props.additionalImages.length > 0) {
    // 在实际应用中，这里会处理下载逻辑
    alert('壁纸下载功能已触发！')
  }
}

// 分享动物
const shareAnimal = () => {
  // 在实际应用中，这里会处理分享逻辑
  alert('分享功能已触发！')
}

// 组件挂载时初始化
onMounted(() => {
  initializeComponent()
})
</script>

<style scoped>
.member-exclusive-content {
  margin-top: 2rem;
  border-radius: 12px;
  overflow: hidden;
  box-shadow: 0 2px 10px rgba(0, 0, 0, 0.1);
}

/* 会员可见内容样式 */
.exclusive-content {
  background-color: #fff;
  padding: 2rem;
}

.content-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 1.5rem;
  padding-bottom: 1rem;
  border-bottom: 2px solid #f0f0f0;
}

.content-header h3 {
  margin: 0;
  color: #333;
  font-size: 1.5rem;
}

.membership-badge {
  padding: 0.25rem 0.75rem;
  border-radius: 20px;
  font-size: 0.85rem;
  font-weight: bold;
  color: white;
}

/* 会员专享内容区块 */
.exclusive-body {
  display: flex;
  flex-direction: column;
  gap: 2rem;
}

.exclusive-section {
  margin-bottom: 1.5rem;
}

.exclusive-section h4 {
  color: #4CAF50;
  margin-bottom: 1rem;
  font-size: 1.2rem;
}

/* 图片库 */
.image-gallery {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(200px, 1fr));
  gap: 1rem;
}

.gallery-image {
  width: 100%;
  height: 150px;
  object-fit: cover;
  border-radius: 8px;
  transition: transform 0.3s ease;
}

.gallery-image:hover {
  transform: scale(1.05);
}

/* 详细信息 */
.detailed-info {
  background-color: #f9f9f9;
  padding: 1rem;
  border-radius: 8px;
}

.fact-item {
  display: flex;
  align-items: flex-start;
  margin-bottom: 0.75rem;
  line-height: 1.5;
}

.fact-item:last-child {
  margin-bottom: 0;
}

.fact-icon {
  margin-right: 0.75rem;
  font-size: 1.2rem;
}

/* 互动功能 */
.interactive-features {
  display: flex;
  gap: 1rem;
  flex-wrap: wrap;
}

.interactive-button {
  padding: 0.75rem 1.5rem;
  border: none;
  border-radius: 8px;
  background-color: #4CAF50;
  color: white;
  font-size: 0.9rem;
  cursor: pointer;
  transition: background-color 0.3s ease;
}

.interactive-button:hover {
  background-color: #45a049;
}

/* 评论区 */
.comments-section {
  border-top: 1px solid #eee;
  padding-top: 1rem;
}

.no-comments {
  text-align: center;
  color: #999;
  padding: 2rem;
}

.comments-list {
  display: flex;
  flex-direction: column;
  gap: 1rem;
  margin-bottom: 1.5rem;
}

.comment-item {
  display: flex;
  gap: 1rem;
  padding: 1rem;
  background-color: #f9f9f9;
  border-radius: 8px;
}

.comment-avatar {
  width: 40px;
  height: 40px;
  border-radius: 50%;
  background-color: #4CAF50;
  color: white;
  display: flex;
  align-items: center;
  justify-content: center;
  font-weight: bold;
  flex-shrink: 0;
}

.comment-content {
  flex: 1;
}

.comment-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 0.5rem;
}

.comment-user {
  font-weight: bold;
  color: #333;
}

.comment-time {
  font-size: 0.8rem;
  color: #999;
}

.comment-text {
  margin: 0;
  color: #666;
  line-height: 1.4;
}

.comment-form {
  display: flex;
  flex-direction: column;
  gap: 1rem;
}

.comment-input {
  width: 100%;
  min-height: 80px;
  padding: 1rem;
  border: 1px solid #ddd;
  border-radius: 8px;
  resize: vertical;
  font-family: inherit;
}

.add-comment-button {
  align-self: flex-end;
  padding: 0.75rem 1.5rem;
  border: none;
  border-radius: 8px;
  background-color: #4CAF50;
  color: white;
  cursor: pointer;
  transition: background-color 0.3s ease;
}

.add-comment-button:hover {
  background-color: #45a049;
}

/* 非会员内容样式 */
.non-member-content {
  background: linear-gradient(135deg, #f5f7fa 0%, #c3cfe2 100%);
  padding: 3rem 2rem;
  text-align: center;
}

.lock-icon {
  font-size: 4rem;
  margin-bottom: 1.5rem;
}

.non-member-content h3 {
  color: #333;
  margin-bottom: 1rem;
  font-size: 1.8rem;
}

.locked-message {
  color: #666;
  margin-bottom: 2rem;
  font-size: 1.1rem;
}

.membership-preview {
  background-color: rgba(255, 255, 255, 0.8);
  border-radius: 12px;
  padding: 1.5rem;
  margin-bottom: 2rem;
}

.preview-text {
  font-weight: bold;
  color: #333;
  margin-bottom: 1rem;
}

.privileges-preview {
  list-style: none;
  padding: 0;
  margin: 0;
  text-align: left;
  max-width: 300px;
  margin: 0 auto;
}

.privileges-preview li {
  margin-bottom: 0.5rem;
  color: #666;
}

.upgrade-button {
  display: inline-block;
  padding: 1rem 2rem;
  background-color: #4CAF50;
  color: white;
  text-decoration: none;
  border-radius: 8px;
  font-weight: bold;
  transition: background-color 0.3s ease;
}

.upgrade-button:hover {
  background-color: #45a049;
}

/* 响应式设计 */
@media (max-width: 768px) {
  .exclusive-content {
    padding: 1.5rem;
  }
  
  .content-header {
    flex-direction: column;
    gap: 1rem;
    text-align: center;
  }
  
  .image-gallery {
    grid-template-columns: repeat(2, 1fr);
  }
  
  .interactive-features {
    flex-direction: column;
  }
  
  .comment-item {
    flex-direction: column;
    align-items: flex-start;
  }
  
  .non-member-content {
    padding: 2rem 1rem;
  }
}
</style>